package com.wang.myapplication.fragment;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.scwang.smartrefresh.header.WaveSwipeHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.BallPulseFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.wang.myapplication.R;
import com.wang.myapplication.activity.MyHttpback;
import com.wang.myapplication.base.BaseFragment;
import com.wang.myapplication.base.MyApp;
import com.wang.myapplication.base.MyBaseAdapter;
import com.wang.myapplication.network.HttpRequest;
import com.wang.myapplication.network.bean.ClassInfoListResponse;
import com.wang.myapplication.network.bean.WildConfigResponse;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;
import com.wang.myapplication.network.bean.ClassInfoListResponse.Classlist;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by xhwang on 2018/5/21.
 */

public class DocumentFragment extends BaseFragment {
    @BindView(R.id.listview)
    ListView listView;
    @BindView(R.id.refreshLayout)
    RefreshLayout refreshLayout;
    private MyBaseAdapter<Classlist> mAdapter;

    @Override
    public int bindLayout() {
        return R.layout.fragment_documnet;
    }

    @Override
    public void initView(View view) {
        refreshLayout.setRefreshHeader(new WaveSwipeHeader(getContext()));
        refreshLayout.setRefreshFooter(new BallPulseFooter(getContext()));
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                reqClassInfoList(true);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                reqClassInfoList(false);
            }
        });

        refreshLayout.autoRefresh();
        Banner banner = view.findViewById(R.id.banner);
        List<String> imageUrls = new ArrayList<>();
        List<WildConfigResponse.Loopimage> loopimages = WildConfigResponse.GetLoopimage();
        imageUrls.add(loopimages.get(5).getNormalimage());
        imageUrls.add(loopimages.get(6).getNormalimage());
        imageUrls.add(loopimages.get(7).getNormalimage());
        imageUrls.add(loopimages.get(8).getNormalimage());
        banner.setImages(imageUrls).setImageLoader(new ImageLoader() {
            @Override
            public void displayImage(Context context, Object path, ImageView imageView) {
                /**
                 注意：
                 1.图片加载器由自己选择，这里不限制，只是提供几种使用方法
                 2.返回的图片路径为Object类型，由于不能确定你到底使用的那种图片加载器，
                 传输的到的是什么格式，那么这种就使用Object接收和返回，你只需要强转成你传输的类型就行，
                 切记不要胡乱强转！
                 */
                ////Glide 加载图片简单用法
                Glide.with(context).load(path).into(imageView);
            }

            //提供createImageView 方法，如果不用可以不重写这个方法，主要是方便自定义ImageView的创建
            //@Override
            //public ImageView createImageView(Context context) {
            //    return super.createImageView(context);
            //
            //}
        }).start();


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "我是item的监听" + position, Toast.LENGTH_SHORT).show();
            }
        });

        mAdapter = new MyBaseAdapter<Classlist>(R.layout.lviewitem_classinfo) {
            @Override
            public void handItemView(final ViewHolder holder, Classlist pClasslist) {
                TextView tvClassName = holder.findViewById(R.id.tvClassName);
                TextView tvClassCount = holder.findViewById(R.id.tvClassCount);
                ImageView imgIcon = holder.findViewById(R.id.imgIcon);

                tvClassName.setText(pClasslist.getName());
                tvClassCount.setText(pClasslist.getFiletypeid() + "");
                Glide.with(mContext).load(pClasslist.viewpath).into(imgIcon);
                imgIcon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(MyApp.getAppContext(), "啊哈哈" + holder.getItemPosition(), Toast.LENGTH_SHORT).show();
                    }
                });

            }
        };
        listView.setAdapter(mAdapter);
    }

    @Override
    public void doBusiness(Context mContext) {
    }

    @Override
    public void widgetClick(View v) {

    }


    private void reqClassInfoList(final boolean isRefresh) {
        if (isRefresh)
            HttpRequest.reqClassInfoList(new MyHttpback<ClassInfoListResponse>() {
                @Override
                public void onSuccessBack(ClassInfoListResponse obj) {
                    List<Classlist> dataLists=obj.transferDatalists(obj);
                    mAdapter.replaceDataList(dataLists);
                    completeRefresh(isRefresh);
                }
            });


    }

    private void completeRefresh(boolean isRefresh) {
        if (isRefresh)
            refreshLayout.finishRefresh();
        else
            refreshLayout.finishLoadmore();
    }


}
